package edu.chalmers.trec;

import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;

import edu.chalmers.targine.ARManager;
import edu.chalmers.targine.CocosComponent;

/**Interface for steering the player's character*/
public class Button implements CocosComponent{
	//Fields
	CCSprite _sprite;
	int _touchID;
	private String _message;
	private boolean available;
	private int _animationState = 0;
	
	/**Constructor initializes graphics, position and touch*/
	public Button(String imageFile, float startX, float startY, String message){
		_sprite = CCSprite.sprite(imageFile);
		_sprite.setPosition(CGPoint.ccp(startX, startY));
		_message = message;
		_touchID = -1;
		available = true;
//		_sprite.setVisible(false);
	}
	
	public void update(float dt, ARManager arManager){
		if(_animationState == 1){
			_sprite.setScale(_sprite.getScale() - 7 * dt);
			if(_sprite.getScale() <= 0.7f){
				_animationState = 2;
			}
		} else if(_animationState == 2 && _touchID == -1){
			_animationState = 3;
		} else if(_animationState == 3){
			_sprite.setScale(_sprite.getScale() + 7 * dt);
			if(_sprite.getScale() >= 1f){
				_animationState = 0;
				_sprite.setScale(1f);
				arManager.performTapAction(_message, null);
			}
		}
	}
	
	/** */
	public void setImage(String image){
//		_sprite = CCSprite.cc;
//		if(_contentFrames.containsKey(image)){
//			_sprite.setDisplayFrame(_contentFrames.get(image));
//			_sprite.setVisible(true);
//		} else {
//			_sprite.setVisible(false);
//		}
	}
	
	/** */
	public CCSprite getSprite(){
		return _sprite;
	}
	
	/** */
	public CGRect getRectangle(){
		return _sprite.getBoundingBox();
	}
	
	/** */
	public boolean isIntersecting(CGRect otherRect){
		return CGRect.intersects(_sprite.getBoundingBox(), otherRect) &&
				_sprite.getVisible() && available;
	}
	
	/** */
	public boolean hasTouchID(int touchID){
		return _touchID == touchID;
	}
	
	/** */
	public void touchBegan(int touchID){
		_touchID = touchID;
	}
	
	/** */
	public int getTouchID(){
		return _touchID;
	}
	
	/** */
	public CGPoint getCenter(){
		return CGPoint.ccp(98, 98);
	}
	
	/** */
	public float getAngleTo(CGPoint point){
		return (float)Math.atan2(
				point.y - _sprite.getPosition().y,
				point.x - _sprite.getPosition().x);
	}
	
	public void touchBegan(int touchID, CGPoint touchPoint) {
		_animationState = 1;
		_touchID = touchID;
	}

	public void updateTouch(int touchID, CGPoint touchPoint) {
		if(hasTouchID(touchID)){
			
		} else{
			_touchID = touchID;
		}
	}

	public void touchEnded(int touchID, ARManager arManager) {
//		arManager.performTapAction(_message, null);
		_touchID = -1;
	}

	public CCNode getNode() {
		return _sprite;
	}
	
	/** */
	public void setVisible(boolean visible){
		_sprite.setVisible(visible);
	}
	
	public boolean isVisible(){
		return _sprite.getVisible();
	}
	
	/** */
	public void setAvailable(boolean available){
		this.available = available;
		if(available){
			_sprite.setOpacity(255);
		} else{
			_sprite.setOpacity(50);
		}
	}
}
